package org.h.aibuildplatform.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

public class JacksonUtils {
    static ObjectMapper objectMapper = new ObjectMapper();
    static {
        //忽略json中的多余属性（java对象中不存在的对象）
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    }

    static public String objectToJson(Object o) throws JsonProcessingException {
        return objectMapper.writeValueAsString(o);
    }

    static public <T> T jsonToObject(String s, Class<T> c) throws JsonProcessingException {
        T t = objectMapper.readValue(s, c);
        return t;
    }


    static public <T> List<T> jsonToList(String s, Class<T> c) throws JsonProcessingException {
        return objectMapper.readValue(s, new TypeReference<List<T>>(){});
    }
}
